package com.bristua.mall.user.biz;

import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.user.dao.TUserAdressMapper;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserAdress;
import com.bristua.mall.user.model.condition.TUserAdressExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/3/24.
 */
@Service
@Transactional
public class UpdateUserDefaultTakeDeliveryBiz {

    @Resource
    private TUserAdressMapper tUserAdressMapper;

    @Autowired
    private CacheUtil cacheUtil;


    public void updateUserDefaultTakeDelivery(TUserAdress tUserAdress){

        if(tUserAdress!=null){
            if(tUserAdress.getStatus().intValue()==1){
                TUser userSession = (TUser) cacheUtil.getUserSession();
                TUserAdressExample tUserAdressExample = new TUserAdressExample();
                tUserAdressExample.createCriteria().andUserIdEqualTo(userSession.getUserId());
                List<TUserAdress> tUserAdresses = tUserAdressMapper.selectByExample(tUserAdressExample);
                if(tUserAdresses!=null && tUserAdresses.size()>0){
                    for(TUserAdress updateUserAddress : tUserAdresses){
                        updateUserAddress.setStatus(0);
                        tUserAdressMapper.updateByPrimaryKeySelective(updateUserAddress);
                    }
                }
            }
            tUserAdressMapper.updateByPrimaryKeySelective(tUserAdress);
        }


    }
}
